module.exports = (api, options) => {
  const isMaster = options.type === "master";
  api.postProcessFiles((files) => {
    if (!isMaster) {
      initSubApp(files, api);
    } else {
      initMainApp(files, api);
    }
  });
  if (isMaster) {
    api.extendPackage({
      dependencies: {
        qiankun: "^2.4.2",
      },
    });
    api.render("./template/mian");
  } else {
    api.render("./template/slave");
  }
};

function initMainApp(files, api) {
  const routerFile = "src/router/index.js";
  let routerStr = files[routerFile];
  if (routerStr) {
    if (routerStr.test(/mode:(['"\w]+),/)) {
      files[routerFile] = routerStr.replace(
        /mode: ?[^\n]+/,
        `mode: "history",`
      );
    } else {
      files[routerFile] = routerStr.replace(
        /(routes: ?[^\n]+)/,
        `mode: "history",\n$1`
      );
    }
  }
}

function initSubApp(files, api) {
  initSubAppEnv(files, api.rootOptions.projectName);
  const routerFile = "src/router/index.js";
  let routerStr = files[routerFile];
  const layoutFile = "src/router/routes.js";
  let layoutStr = files[layoutFile];
  if (routerStr) {
    routerStr = routerStr.replace(
      /base: (?[^\n]+)?/,
      `base: window.__POWERED_BY_QIANKUN__ ? "/" + process.env.VUE_APP_SUBAPPNAME : $1`
    );
  }
  if (layoutStr) {
    // 乾坤下隐藏 框架
    files[layoutFile] = layoutStr.replace(
      /import EmptyLayout from ['"\w@]*\/components\/layout\/EmptyLayout.vue['";]*/g,
      ""
    );
    files[layoutFile] = layoutStr.replace(
      /import Layout from ['"\w@]*\/components\/layout\/Layout.vue['";]*/,
      `import MainLayout from "@/components/layout/Layout.vue";\nimport EmptyLayout from "@/components/layout/EmptyLayout.vue";
let Layout = MainLayout;
if (window.__POWERED_BY_QIANKUN__) {
  Layout=EmptyLayout;
}\n`
    );
  }
  if (files[api.entryFile]) {
    initSubMain(files, api.entryFile);
  }
}

function initSubAppEnv(files, name = "app1") {
  const env = files[".env"];
  if (!env) {
    files[".env"] = "VUE_APP_SUBAPPNAME=" + name;
  } else if (env.indexOf("VUE_APP_SUBAPPNAME=") === -1) {
    files[".env"] = env + "VUE_APP_SUBAPPNAME=" + name;
  }
}

function initSubMain(files, entryFile) {
  const replaceStr = `let instance = null;
function render () {
  instance = $1
}
if (!window.__POWERED_BY_QIANKUN__) {
  render();
}
export async function bootstrap () {
  console.log("子应用启动");
}
export async function mount (props) {
  console.log("主应用参数", props);
  // 父子应用通信
  props.onGlobalStateChange((state, prev) => {
    console.log(state, prev);
  });
  Vue.prototype.setQiankunGlobalState = props.setGlobalState.bind(props);
  render();
}
export async function unmount () {
  instance.$destroy();
  instance = null;
}
`;
  let entryStr = files[entryFile] || "";
  if (entryStr.indexOf("export async function mount") === -1) {
    entryStr = entryStr.replace(
      /\$mount\(['"]#app['"]\)/,
      `$mount("#main-app")`
    );
    entryStr = entryStr.replace(
      /import Vue from [^\n]*/,
      `import Vue from "vue";\nimport "./public-path.js";`
    );
    files[entryFile] = entryStr.replace(
      /[^\n]*(new Vue\([\s\S]*)/,
      replaceStr
    );
  }
  let appFile = "public/index.html";
  let appStr = files[appFile];
  if(appStr) {
    files[appFile] = appStr.replace(/ id="app"/, ` id="main-app"`);
  }
  let cssFile = "src/styles/index.less";
  let cssStr = files[cssFile];
  if(cssStr) {
    files[appFile] = cssStr.replace(/#app ?{/, `#main-app {`);
  }
}
